<?php

class ComNoteEditPanel extends QPanel {
    // Local instance of the ComNoteMetaControl
    public $tabMain;
    protected $mctComNote;

    // Controls for ComNote's Data Fields
    public $lstTypeObject;
    public $txtNotes;
    public $calDate;


    public $btnSave;
    public $btnDelete;
    public $btnCancel;


    // Callback
    protected $strClosePanelMethod;

    public function __construct($objParentObject, $strClosePanelMethod, $intId = null, $strControlId = null) {
        // Call the Parent
        try {
            parent::__construct($objParentObject, $strControlId);
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }

        // Setup Callback and Template
        $this->strTemplate = __DOCROOT__.__COM__ .  '/tpl/QePanel_edit.tpl.php';
        $this->strClosePanelMethod = $strClosePanelMethod;

        //MCT
        $this->mctComNote = ComNoteMetaControl::Create($this, $intId);

        //Create the Record Info for this record
        $objEditPanel = new ComNoteInfoPanel ($this, $this->mctComNote);
        $this->objForm->SetEditPane($objEditPanel, "pnlComNoteInfo");

        $this->calDate = $this->mctComNote->calDate_Create();
        $this->lstTypeObject = $this->mctComNote->lstTypeObject_Create();

        $this->txtNotes = $this->mctComNote->txtNotes_Create();
        $this->txtNotes->Width = "700";
        $this->txtNotes->Height = "100";

        // Create Buttons and Actions on this Form
        $this->btnSave = new QButton($this);
        $this->btnSave->Text = QApplication::Translate('Save');
        $this->btnSave->Name = QApplication::Translate('Save');
        $this->btnSave->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnSave_Click'));
        $this->btnSave->CausesValidation = $this;

        $this->btnCancel = new QButton($this);
        $this->btnCancel->Text = QApplication::Translate('Cancel');
        $this->btnCancel->Name = QApplication::Translate('Cancel');
        $this->btnCancel->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnCancel_Click'));

        $this->btnDelete = new QButton($this);
        $this->btnDelete->Text = QApplication::Translate('Delete');
        $this->btnDelete->Name = QApplication::Translate('Delete');
        $this->btnDelete->AddAction(new QClickEvent(), new QConfirmAction(QApplication::Translate('Are you SURE you want to DELETE this') . ' ' . QApplication::Translate('ComNote') . '?'));
        $this->btnDelete->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnDelete_Click'));
        $this->btnDelete->Visible = $this->mctComNote->EditMode;

        //change tab
        $this->objForm->tabMain->ChangeTabById(1,1,1);

    }

    // Control AjaxAction Event Handlers
    public function btnSave_Click($strFormId, $strControlId, $strParameter) {
        // Delegate "Save" processing to the ComNoteMetaControl
        $this->mctComNote->SaveComNote();
        $this->CloseSelf(true);
    }

    public function btnDelete_Click($strFormId, $strControlId, $strParameter) {
        // Delegate "Delete" processing to the ComNoteMetaControl
        $this->CloseSelf();
        $this->mctComNote->DeleteComNote();

    }

    public function btnCancel_Click($strFormId, $strControlId, $strParameter) {
        $this->CloseSelf();
    }

    // Close Myself and Call ClosePanelMethod Callback
    protected function CloseSelf($blnChangesMade=false) {
        $strMethod = $this->strClosePanelMethod;
        //Close Panel Info
        $this->objForm->$strMethod("pnlComNoteInfo");
        $pnlRefresh=null;
        if ($blnChangesMade) $pnlRefresh="pnlComNoteList";
        //Close Panel Edit and update panel note list if needed
        $this->objForm->$strMethod("pnlComNoteEdit",$pnlRefresh);
        //change tab
        $this->objForm->tabMain->ChangeTabById(1,1,0);

    }

    public function Validate() {
        $blnToReturn = true;
        if ($this->lstTypeObject->SelectedValue == "") {
            $this->lstTypeObject->Warning="Could not be empty";
            $this->lstTypeObject->Focus();
            $this->lstTypeObject->Blink();
            $blnToReturn=false;
        }
        //if ()
        if ($this->txtNotes->Text == "" ) {
            $this->txtNotes->Warning="Could not be empty";
            $this->txtNotes->Focus();
            $this->txtNotes->Blink();
            $blnToReturn=false;
        }

        return $blnToReturn;
    }


}
?>